package ch.fhnw.kosmonauten.client.gui;


import javax.swing.JOptionPane;

import ch.fhnw.kosmonauten.client.net.ServerConnection;
import ch.fhnw.kosmonauten.model.Category;
import ch.fhnw.kosmonauten.model.User;
import ch.fhnw.kosmonauten.net.Request;
import ch.fhnw.kosmonauten.net.Response;
import ch.fhnw.kosmonauten.net.TextMessage;



/**
 * Diese Klasse ermoeglicht die erfassung eines neuen Dossiers.
 * 
 * @author Fabian Siegrist
 * @author Oliver Freiermuth
 * @author Mikhail Onoufrienko
 * @author Risto Milani
 * @version $Revision: 153 $
 *          File name: $HeadURL: http://iching-kosmonauten.googlecode.com/svn/I-Ching/src/ch/fhnw/kosmonauten/client/gui/NewDossier.java $
 *          Last modified:$Date: 2013-05-25 22:43:20 +0000 (Sat, 25 May 2013) $
 *          Last modified by: $Author: molotok.mihon@gmail.com $
 */

// class definition
public class NewDossier extends javax.swing.JFrame {

	// attributes
	private final MainWindow main;
	private ServerConnection sc;
	private Category[] categories;
	private User user;

	// constructor
	/**
	 * Creates new form NewDosser
	 * 
	 * @param mw
	 */
	public NewDossier(MainWindow mw, User user) {

		this.main = mw;
		this.user = user;
		initComponents();
		this.setVisible(true);
		
		
	}

	// methods
	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	/**
	 * 
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jTextField1 = new javax.swing.JTextField();
		jComboBox7 = new javax.swing.JComboBox();
		jRadioButton2 = new javax.swing.JRadioButton();
		jRadioButton1 = new javax.swing.JRadioButton();
		jButton1 = new javax.swing.JButton();
		bGroup = new javax.swing.ButtonGroup();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		System.out.println(user.getRole());
		setTitle(user.displayValue("mw37"));

		jLabel1.setText(user.displayValue("mw38"));

		jLabel2.setText(user.displayValue("mw39"));

		jTextField1.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jTextField1ActionPerformed(evt);
			}
		});

		jButton1.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		// Kategorien aus der Datenbank holen
		sc = new ServerConnection();
		Response resp = sc.sendRequest(new Request(new TextMessage(
				"getCategories"), "getCategories"));
		if (resp == null)
			System.out.println(user.displayValue("mw40"));
		else if (resp.getContent() instanceof Category[]) {
			categories = (Category[]) resp.getContent();
			if (categories.length == 0)
				System.out.println(user.displayValue("mw41"));
			else {
				javax.swing.DefaultComboBoxModel cbmodel = new javax.swing.DefaultComboBoxModel();
				for (Category cat : categories)
					cbmodel.addElement("[" + cat.getCategoryID() + "] - "
							+ cat.getCategoryName());
				jComboBox7.setModel(cbmodel);
			}
		} else
			System.out.println(resp.getContent().toString());
		// ENDE der DB

		jRadioButton2.setText(user.displayValue("mw42"));
		jRadioButton2.setSelected(true);
		jRadioButton1.setText(user.displayValue("mw43"));

		bGroup.add(jRadioButton2);
		bGroup.add(jRadioButton1);

		jButton1.setText(user.displayValue("mw44"));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		jRadioButton1)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		Short.MAX_VALUE)
																.addComponent(
																		jButton1))
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addGroup(
																						layout.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								false)
																								.addGroup(
																										layout.createSequentialGroup()
																												.addComponent(
																														jLabel1)
																												.addGap(28,
																														28,
																														28)
																												.addComponent(
																														jTextField1,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														312,
																														javax.swing.GroupLayout.PREFERRED_SIZE))
																								.addGroup(
																										layout.createSequentialGroup()
																												.addComponent(
																														jLabel2)
																												.addPreferredGap(
																														javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																												.addComponent(
																														jComboBox7,
																														0,
																														javax.swing.GroupLayout.DEFAULT_SIZE,
																														Short.MAX_VALUE)))
																				.addComponent(
																						jRadioButton2))
																.addGap(0,
																		0,
																		Short.MAX_VALUE)))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel1)
												.addComponent(
														jTextField1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel2)
												.addComponent(
														jComboBox7,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addComponent(jRadioButton2)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jRadioButton1)
												.addComponent(jButton1))
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));

		pack();
	}// </editor-fold>

	/**
	 * @param evt
	 */
	private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
	}

	/**
	 * @param evt
	 */
	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

		if (this.jTextField1.getText().length() > 2) {
			this.dispose();// TODO add your handling code here:
			main.setEmptyDossier();
			main.initComponents(this.jTextField1.getText(),
					(String) jComboBox7.getSelectedItem(),
					(jRadioButton1.isSelected() ? 1 : 0));
		} else
			new ErrorDialog(0,JOptionPane.ERROR_MESSAGE,user);

	}

	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JComboBox jComboBox7;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JRadioButton jRadioButton1;
	private javax.swing.JRadioButton jRadioButton2;
	private javax.swing.JTextField jTextField1;
	private javax.swing.ButtonGroup bGroup;
	// End of variables declaration
}
